#include "IOServer.h"

void DigitalInput::toggle()
{
    state = manual ? !state : state;
    webGuiServer->send ( 200, "text/plain", "");
}

void DigitalInput::getState()
{
    const char* high = mode == INPUT_PULLUP ? "OFF" : "ON";
    const char* low = mode == INPUT_PULLUP ? "ON" : "OFF";
    webGuiServer->send(200, "text/plain", state ? high : low);
}

void DigitalInput::setOverride()
{
    manual = !manual;
    webGuiServer->send ( 200, "text/plain", "");
}

void DigitalInput::getOverride()
{
    webGuiServer->send(200, "text/plain", manual ? "MANUAL" : "AUTO");
}

void DigitalInput::init()
{
    if (mode != WEB_IO_LOGIC)
    {
        pinMode(pin, mode);
    }
    webGuiServer->on(setValueUri.c_str(),[&]{toggle();});
    webGuiServer->on(getValueUri.c_str(),[&]{getState();});
    webGuiServer->on(setOverrideUri.c_str(),[&]{setOverride();});
    webGuiServer->on(getOverrideUri.c_str(),[&]{getOverride();});
}

void DigitalInput::update()
{
    if (mode != WEB_IO_LOGIC)
    {
        state = manual ? state : digitalRead(pin);
    }
}

void DigitalInput::createWidget(std::string name, int interval)
{
    loopFunc = "setInterval(" + name + "Loop, " + std::to_string(interval) + ");";
    
    HTML = "<div style=\"border:1px solid black; width:100px; float:left; margin-left: 30px;\">\
                <p>" + name + "</p>\
                <p id=\"" + name + "_state\"></p>\
                <p id=\"" + name + "_control\"></p>\
                <input id=\"" + name + "_manual\" type=\"button\" value=\"Override\" onclick=\"set_" + name + "_manual()\"/><br>\
                <input id=\"" + name + "_toggle\" type=\"button\" value=\"Toggle\" onclick=\"" + name + "_toggle()\"/>\
            </div>";
    
    JS = "\
        function " + name + "Loop() {\
            document.getElementById(\"" + name + "_control\").innerHTML = get_" + name + "_manual();\
            document.getElementById(\"" + name + "_state\").innerHTML = get_" + name + "_state();\
        }\
        function set_" + name + "_manual() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open( \"GET\", \"/set_override_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }\
        function get_" + name + "_manual() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open( \"GET\", \"/get_override_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }\
        function " + name + "_toggle() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open( \"GET\", \"/set_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }\
        function get_" + name + "_state() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open( \"GET\", \"/get_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }";
}

DigitalInput::DigitalInput(std::string name, int interval)
{
    ioType = DI;
    setUri(name);
    this->mode = WEB_IO_LOGIC;
    createWidget(name, interval);
}

DigitalInput::DigitalInput(std::string name, uint8_t pin, uint8_t mode, int interval)
{
    ioType = DI;
    setUri(name);
    this->pin = pin;
    this->mode = mode;
    createWidget(name, interval);
}

